package cn.edu.hubu.javaparkinglot.service.impl;

import cn.edu.hubu.javaparkinglot.VO.ResultVO;
import cn.edu.hubu.javaparkinglot.entity.Seat;
import cn.edu.hubu.javaparkinglot.mapper.SeatMapper;
import cn.edu.hubu.javaparkinglot.service.SeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SeatServiceImpl implements SeatService {

    @Autowired
    private SeatMapper seatMapper; // 在Service业务中注入一个SeatMapper对象

    @Override
    public ResultVO getSeatById(int seatId) {
        // 编写业务代码
        // 1. 调用SeatMapper中的selectBySeat_id方法按照seatId参数查询座位信息
        Seat seat = seatMapper.selectBySeat_id(seatId);

        // 2. 根据查询结果进行响应
        if (seat != null) {
            return new ResultVO(200, "查询到车位", seat);
        } else {
            return new ResultVO(500, "未查询到车位", null);
        }
    }

    @Override
    public ResultVO saveSeat(Seat seat) {
        // 编写业务代码
        // 1. 调用insertSeat()方法添加车位信息
        int result = seatMapper.insertSeat(seat);

        // 2. 根据结果进行响应
        if (result > 0) {
            return new ResultVO(200, "添加成功", seat);
        } else {
            return new ResultVO(500, "添加失败", null);
        }
    }

    @Override
    public ResultVO deleteSeat(int seatId) {
        // 编写业务代码
        // 1. 调用deleteSeat()方法删除座位信息
        int result = seatMapper.deleteSeat(seatId);

        // 2. 根据结果进行响应
        if (result > 0) {
            return new ResultVO(200, "删除成功", null);
        } else {
            return new ResultVO(500, "删除失败或座位不存在", null);
        }
    }

    @Override
    public ResultVO updateSeat(Seat seat) {
        // 编写业务代码
        // 1. 调用modifySeat()方法修改座位信息
        int result = seatMapper.modifySeat(seat);

        // 2. 根据结果进行响应
        if (result > 0) {
            return new ResultVO(200, "修改成功", seat);
        } else {
            return new ResultVO(500, "修改失败或座位不存在", null);
        }
    }
}